package com.example.shop.modules.sys.controller;


import com.example.shop.dto.CartParam;
import com.example.shop.modules.sys.service.ICartService;
import com.example.shop.persistence.entity.Cart;
import com.example.shop.utils.JSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.example.shop.utils.JSONResult.renderSuccess;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author jason
 * @since 2019-12-17
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/sys/cart")
public class CartController {

    @Autowired
    private ICartService cartService;

    @PostMapping
    @ApiOperation(value = "新增或修改")
    public JSONResult addCart(CartParam cartParam){
        cartService.saveCart(cartParam);
        return renderSuccess();
    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    public JSONResult deleteCart(String ids){
        return renderSuccess(cartService.deleteCarts(ids));
    }

    @GetMapping
    @ApiOperation(value = "查询", notes = "分页条件查询")
    public JSONResult selectListCart(Integer page, Integer pageSize, Integer userID, Integer isCollect){
        return renderSuccess(cartService.selectListCart(page,pageSize,userID, isCollect));
    }

    @GetMapping("/number")
    @ApiOperation(value = "查询购物车数量", notes = "商品种类")
    public JSONResult selectContCart(Integer userID){
        return renderSuccess(cartService.selectContCart(userID));
    }

    @GetMapping("/include")
    @ApiOperation(value = "查询购物车中是否包含某件商品", notes = "查询某件商品是否已经在购物车中或已经添加收藏了")
    public JSONResult selectInclude(Integer userID, Integer goodsID, Integer isCollect){
        return renderSuccess(cartService.selectInclude(userID, goodsID, isCollect));
    }

    @PostMapping("/add")
    @ApiOperation(value = "新增或修改，单条记录")
    public JSONResult addOrUpdateCart(Cart cart){
        return renderSuccess(cartService.saveOrUpdate(cart));
    }

    @GetMapping("/delete")
    @ApiOperation(value = "根据商品ID删除")
    public  JSONResult deleteCart(Integer userID, Integer goodsID, Integer isCollect){
        return renderSuccess(cartService.deleteCart(userID, goodsID, isCollect));
    }
}

